﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Resume.IRepository;
using Resume.Model;
using Resume.ViewModels;

namespace Resume.System.Controllers;

/// <summary>
/// FieldSetting
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class FieldSettingController : ControllerBase
{
    private readonly IFieldSetting_Repository _Repository;

    private readonly IFieldClassDict_Repository _RepositoryClass;

    private readonly IMapper _mapper;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="Repository"></param>
    /// <param name="mapper"></param>
    public FieldSettingController(IFieldSetting_Repository Repository, IFieldClassDict_Repository RepositoryClass, IMapper mapper)
    {
        _Repository = Repository;

        _RepositoryClass = RepositoryClass;

        _mapper = mapper;
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route("[action]")]
    public ActionResult Get(int id)
    {
        var list = _Repository.Get(id);

        var values = _mapper.Map<List<FieldSettingViewModel>>(list);

        return Ok(values);
    }

    /// <summary>
    /// GetSearch
    /// </summary>
    /// <param name="Seachval"></param>
    /// <param name="pageIndex"></param>
    /// <param name="PageSize"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("[action]")]
    public ActionResult GetSearch(string Seachval = "", int pageIndex = 1, int PageSize = 10)
    {
        var lis = _Repository.GetPageData(Seachval, false, pageIndex, PageSize);

        var values = _mapper.Map<Page<FieldSettingDict>, Page<FieldSettingViewModel>>(lis);

        return Ok(values);
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="viewModel"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("[action]")]
    public ActionResult Add(FieldSettingViewModel viewModel)
    {
        var classobj = _RepositoryClass.GetSinge(Convert.ToInt32(viewModel.ObjPID));

        FieldSettingDict model = new FieldSettingDict
        {
            FieldCode = Guid.NewGuid().ToString(),

            FieldClassCode = classobj.FieldClassCode,

            FieldName = viewModel.ObjName,

            FieldRemark = viewModel.Remark,

            CreateDate = DateTime.Now,

            Status = viewModel.Enable ? 1 : 0
        };

        return Ok(_Repository.Add(model));
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="viewModel"></param>
    /// <returns></returns>
    [HttpPut]
    [Route("[action]")]
    public ActionResult Update(FieldSettingViewModel viewModel)
    {
        var model = _Repository.GetSinge(Convert.ToInt32(viewModel.ObjID));

        if (model != null)
        {
            model.FieldName = viewModel.ObjName;

            return Ok(_Repository.Edit(model));
        }
        else
        {
            return BadRequest();
        }
    }

    /// <summary>
    /// 修改状态
    /// </summary>
    /// <param name="ObjID"></param>
    /// <param name="Status"></param>
    /// <returns></returns>
    [HttpPut]
    [Route("[action]")]
    public ActionResult UpdateStatus(int ObjID, bool Status)
    {
        var model = _Repository.GetSinge(ObjID);

        if (model != null)
        {
            model.Status = Status ? 1 : 0;

            return Ok(_Repository.Edit(model));
        }
        else
        {
            return BadRequest();
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="ID"></param>
    /// <returns></returns>
    [HttpDelete]
    [Route("[action]")]
    public ActionResult Delete(int ID)
    {
        var obj = _Repository.GetSinge(Convert.ToInt32(ID));

        return Ok(_Repository.Del(obj));
    }
}
